<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );  

//mm_showMyFileName( __FILE__ );

require_once dirname(__FILE__).'/../../../../libraries/phpExcelReader/ExportExcel/Writer.php';
require_once ( 'ps_database.php');

class vm_ps_exportdata {
    function checkExport($vendor_id) {
        $q = "SELECT c.vendor_id
                FROM #__{vm}_category c
                  LEFT JOIN
                    (SELECT p_c_xref.category_id, p_c_xref.product_id,
                        p.vendor_id, p.product_name, p.product_desc, p_p.product_price,
                        p.product_special, p.attribute
                     FROM #__{vm}_product_category_xref p_c_xref
                        INNER JOIN #__{vm}_product p ON p_c_xref.product_id = p.product_id
                        LEFT JOIN #__{vm}_product_price p_p ON p.product_id = p_p.product_id) vm_p
                  ON c.vendor_id = vm_p.vendor_id AND c.category_id = vm_p.category_id
                WHERE c.vendor_id = ".$vendor_id;

        $db = new ps_DB;       
        $db->query( $q );
        $db->next_record();
        
        $row_count = count($db->record);

        return $row_count;
    }
    
    function processExport($vendor_id) {
        $workbook = new Spreadsheet_Excel_Writer();
        
        $worksheet =& $workbook->addWorksheet();
        
        $q = "SELECT c.vendor_id, c.category_id, c.category_name,
                  vm_p.product_id, vm_p.product_name, vm_p.product_desc, vm_p.product_price, 
                  vm_p.product_special, vm_p.attribute
              FROM #__{vm}_category c
                  INNER JOIN
                    (SELECT p_c_xref.category_id, p_c_xref.product_id,
                        p.vendor_id, p.product_name, p.product_desc, p_p.product_price,
                        p.product_special, p.attribute
                     FROM #__{vm}_product_category_xref p_c_xref
                        INNER JOIN #__{vm}_product p ON p_c_xref.product_id = p.product_id
                        LEFT JOIN #__{vm}_product_price p_p ON p.product_id = p_p.product_id) vm_p
                  ON c.vendor_id = vm_p.vendor_id AND c.category_id = vm_p.category_id
              WHERE c.vendor_id = ".$vendor_id."
              ORDER BY c.category_id, vm_p.product_id";

        $db = new ps_DB;       
        $db->query( $q );
        $db->next_record();
        
        $iRowIndex = 0;
        // Header
        $worksheet->write($iRowIndex, 0, 'ID');
        $worksheet->write($iRowIndex, 1, 'Subject / Menu Item');
        $worksheet->write($iRowIndex, 2, 'Menu Item Description');
        $worksheet->write($iRowIndex, 3, 'Add Ons/Cost');
        $worksheet->write($iRowIndex, 4, 'Cost');
        $worksheet->write($iRowIndex, 5, 'Item Special');
        
        $row_count = count($db->record);
        
        if ($row_count == 0) {
            $arrList = ps_exportdata::GetDataDemo();
            $category_name_old = '';
            for($i=0; $i<count($arrList); $i++) {
                // Insert category
                if ($category_name_old != $arrList[$i]['category_name']) {
                    $iRowIndex++;
                    $worksheet->write($iRowIndex, 0, $arrList[$i]['category_id']);
                    $worksheet->write($iRowIndex, 1, $arrList[$i]['category_name']);
                }
                
                // Insert product
                $iRowIndex++;
                
                // Id
                $worksheet->write($iRowIndex, 0, $arrList[$i]['product_id']);
                // Product Name
                $worksheet->write($iRowIndex, 1, $arrList[$i]['product_name']);
                // Product Desc
                $worksheet->write($iRowIndex, 2, $arrList[$i]['product_desc']);
                // Attribute
                $worksheet->write($iRowIndex, 3, $arrList[$i]['product_attribute']);
                // Product Price
                $worksheet->write($iRowIndex, 4, $arrList[$i]['product_price']);
                // Product Special
                $worksheet->write($iRowIndex, 5, $arrList[$i]['product_special']);
                
                $category_name_old = $arrList[$i]['category_name'];
            }                        
        } else {
            $category_id_old = 0;
            for ($i = 0; $i < $row_count; $i++) {
                $obj = $db->record[$i];
                
                // Insert category
                if ($category_id_old != $obj->category_id) {
                    $iRowIndex++;
                    $worksheet->write($iRowIndex, 0, $obj->category_id);
                    $worksheet->write($iRowIndex, 1, $obj->category_name);
                }
                
                // Insert product
                $iRowIndex++;
                
                // Id
                $worksheet->write($iRowIndex, 0, $obj->product_id);
                // Product Name
                $worksheet->write($iRowIndex, 1, $obj->product_name);
                // Product Desc
                $worksheet->write($iRowIndex, 2, $obj->product_desc);
                // Attribute
                $worksheet->write($iRowIndex, 3, $obj->attribute);
                // Product Price
                $worksheet->write($iRowIndex, 4, $obj->product_price);
                // Product Special
                $worksheet->write($iRowIndex, 5, $obj->product_special);
                                
                $category_id_old = $obj->category_id;
            }
        }
        
        $sCurrDate = date('dmYHis');
        // Send file to client
        $workbook->send($sCurrDate.'.xls');
        $workbook->close();
    }
    
    function GetDataDemo() {
        $arrList = array();
        
        // Appetizer - Start
        $index = 0;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Appetizer";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 1";
        $arrList[$index]['product_desc'] = "Item 1 description";
        $arrList[$index]['product_price'] = "6";
        $arrList[$index]['product_special'] = "Y";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Appetizer";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 2";
        $arrList[$index]['product_desc'] = "Item 2 description";
        $arrList[$index]['product_price'] = "7";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Appetizer";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 3";
        $arrList[$index]['product_desc'] = "Item 3 description";
        $arrList[$index]['product_price'] = "7.15";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        // Appetizer - End
        
        // Soups - Start
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Soups";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 1";
        $arrList[$index]['product_desc'] = "Item 1 description";
        $arrList[$index]['product_price'] = "6";
        $arrList[$index]['product_special'] = "Y";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Soups";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 2";
        $arrList[$index]['product_desc'] = "Item 2 description";
        $arrList[$index]['product_price'] = "7";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Soups";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 3";
        $arrList[$index]['product_desc'] = "Item 3 description";
        $arrList[$index]['product_price'] = "7.15";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        // Soups - End
        
        // Salads - Start
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Salads";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 1";
        $arrList[$index]['product_desc'] = "Item 1 description";
        $arrList[$index]['product_price'] = "6";
        $arrList[$index]['product_special'] = "Y";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Salads";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 2";
        $arrList[$index]['product_desc'] = "Item 2 description";
        $arrList[$index]['product_price'] = "7";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Salads";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 3";
        $arrList[$index]['product_desc'] = "Item 3 description";
        $arrList[$index]['product_price'] = "7.15";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        // Salads - End
        
        // Lunch - Start
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Lunch";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 1";
        $arrList[$index]['product_desc'] = "Item 1 description";
        $arrList[$index]['product_price'] = "6";
        $arrList[$index]['product_special'] = "Y";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Lunch";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 2";
        $arrList[$index]['product_desc'] = "Item 2 description";
        $arrList[$index]['product_price'] = "7";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Lunch";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 3";
        $arrList[$index]['product_desc'] = "Item 3 description";
        $arrList[$index]['product_price'] = "7.15";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        // Lunch - End
        
        // Steaks & Chops - Start
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Steaks & Chops";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 1";
        $arrList[$index]['product_desc'] = "Item 1 description";
        $arrList[$index]['product_price'] = "6";
        $arrList[$index]['product_special'] = "Y";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Steaks & Chops";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 2";
        $arrList[$index]['product_desc'] = "Item 2 description";
        $arrList[$index]['product_price'] = "7";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Steaks & Chops";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 3";
        $arrList[$index]['product_desc'] = "Item 3 description";
        $arrList[$index]['product_price'] = "7.15";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        // Steaks & Chops - End
        
        // Seafood - Start
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Seafood";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 1";
        $arrList[$index]['product_desc'] = "Item 1 description";
        $arrList[$index]['product_price'] = "6";
        $arrList[$index]['product_special'] = "Y";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Seafood";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 2";
        $arrList[$index]['product_desc'] = "Item 2 description";
        $arrList[$index]['product_price'] = "7";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Seafood";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 3";
        $arrList[$index]['product_desc'] = "Item 3 description";
        $arrList[$index]['product_price'] = "7.15";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        // Seafood - End
        
        // Dessert - Start
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Dessert";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 1";
        $arrList[$index]['product_desc'] = "Item 1 description";
        $arrList[$index]['product_price'] = "6";
        $arrList[$index]['product_special'] = "Y";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Dessert";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 2";
        $arrList[$index]['product_desc'] = "Item 2 description";
        $arrList[$index]['product_price'] = "7";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Dessert";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 3";
        $arrList[$index]['product_desc'] = "Item 3 description";
        $arrList[$index]['product_price'] = "7.15";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        // Dessert - End
        
        // Beverages - Start
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Beverages";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 1";
        $arrList[$index]['product_desc'] = "Item 1 description";
        $arrList[$index]['product_price'] = "6";
        $arrList[$index]['product_special'] = "Y";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Beverages";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 2";
        $arrList[$index]['product_desc'] = "Item 2 description";
        $arrList[$index]['product_price'] = "7";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        
        $index++;
        $arrList[$index]['category_id'] = "";
        $arrList[$index]['category_name'] = "Beverages";
        $arrList[$index]['product_id'] = "";
        $arrList[$index]['product_name'] = "Item 3";
        $arrList[$index]['product_desc'] = "Item 3 description";
        $arrList[$index]['product_price'] = "7.15";
        $arrList[$index]['product_special'] = "N";
        $arrList[$index]['product_attribute'] = "Add Ons 1,Option 1[+2],Option 2[+1],Option 3;Add Ons 2,Option 1[+2],Option 2[+3],Option 3[+2]";
        // Beverages - End
        
        return $arrList;
    }
}  

// Check if there is an extended class in the Themes and if it is allowed to use them
// If the class is called outside Virtuemart, we have to make sure to load the settings
// Thomas Kahl - Feb. 2009

if (!defined('VM_ALLOW_EXTENDED_CLASSES') && file_exists(dirname(__FILE__).'/../virtuemart.cfg.php')) {
    include_once(dirname(__FILE__).'/../virtuemart.cfg.php');
}
// If settings are loaded, extended Classes are allowed and the class exisits...
if (defined('VM_ALLOW_EXTENDED_CLASSES') && defined('VM_THEMEPATH') && VM_ALLOW_EXTENDED_CLASSES && file_exists(VM_THEMEPATH.'user_class/'.basename(__FILE__))) {
    // Load the theme-user_class as extended
    include_once(VM_THEMEPATH.'user_class/'.basename(__FILE__));
} else {
    // Otherwise we have to use the original classname to extend the core-class
    class ps_exportdata extends vm_ps_exportdata {}
}

?>
